﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;

using Pibby.Core.IO;
using Pibby.Core.Persistence;
using Pibby.Core.PortableMedia;
using Pibby.Core.Web;
using dogd;

namespace Pibby.Core
{
	/// <summary>
	/// Locally persisted application-wide settings saved to .xml file (rather than db).
	/// </summary>
	public class ApplicationSettings : IApplicationSettings
	{
		#region Constructor

		public ApplicationSettings()
		{
			this.MainWindow = new MainWindowSettings();
			this.MediaPlayer = new MediaPlayerSettings();
			this.Polling = new PollingSettings();

			this.ShowTooltips = true;
			this.TooltipTimeout = 500;
		}

		public ApplicationSettings(IApplicationSettings settings)
			: this()
		{
			this.DownloadFolder = settings.DownloadFolder;
			this.StartMinimized = settings.StartMinimized;

			this.MainWindow.CurrentView = settings.MainWindow.CurrentView;
			this.MainWindow.Height = settings.MainWindow.Height;
			this.MainWindow.Left = settings.MainWindow.Left;
			this.MainWindow.SplitterDistance = settings.MainWindow.SplitterDistance;
			this.MainWindow.Top = settings.MainWindow.Top;
			this.MainWindow.Width = settings.MainWindow.Width;

			this.RepositoryPath = settings.RepositoryPath;
			this.RefreshAtStartup = settings.RefreshAtStartup;
			this.ShowTooltips = settings.ShowTooltips;
			this.TooltipTimeout = settings.TooltipTimeout;		
			this.Path = settings.Path;

			this.Polling.StartupDelayMinutes = settings.Polling.StartupDelayMinutes;
			this.Polling.PollingIntervalMinutes = settings.Polling.PollingIntervalMinutes;
		}

		public ApplicationSettings(IFolder downloadFolder, IFolder settingsFolder, string dbFileName)
			: this()
		{
			this.DownloadFolder = downloadFolder.Path;
			this.RepositoryPath = settingsFolder.PathToFile(dbFileName);
		}

		#endregion Constructor

		#region Portable Media

		/// <summary>
		/// Media Player settings - which drive, where to place files, etc.
		/// </summary>
		public MediaPlayerSettings MediaPlayer
		{
			get;
			set;
		}

		#endregion

		#region Behaviour

		public bool RefreshAtStartup
		{
			get;
			set;
		}
		
		public bool ShowTooltips
		{
			get;
			set;
		}

		public int TooltipTimeout
		{
			get;
			set;
		}

		#endregion Behaviour

		#region Files and Folders

		/// <summary>
		/// Where to find the database
		/// </summary>
		public string RepositoryPath
		{
			get;
			set;
		}

		/// <summary>
		/// Where downloads will be held before being sent to the media player
		/// </summary>
		public string DownloadFolder
		{
			get;
			set;
		}

		#endregion Files and Folders

		#region Window Properties

		public bool StartMinimized
		{
			get;
			set;
		}

		/// <summary>
		/// Main application window settings - size, location, last selection...
		/// </summary>
		public MainWindowSettings MainWindow
		{
			get;
			set;
		}

		#endregion Window Properties

		#region Polling 

		public PollingSettings Polling
		{
			get;
			set;
		}

		#endregion

		#region Convenience Properties

		/// <summary>
		/// Convenient property containing the path to the file 
		/// - used for saving back again once the file has been loaded
		/// </summary>
		[XmlIgnore]
		public string Path
		{
			get;
			set;
		}

		#endregion Convenience Properties

		/// <summary>
		/// Save the settings to an xml file
		/// </summary>
		public void Save(TextWriter writer)
		{
			XmlPersistence.SaveTo<ApplicationSettings>(writer, this);
		}
	}
}
